package com.yuxi.common;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

/**
 * @author sunke
 * @Date 2020/6/4 22:11
 * @description
 */

public class ImageUtil {


    /**
     * // scale: 缩小的倍数，1代表保持原有的大小(默认1) 范围 1 - 0
     * // outputQuality : 压缩的质量，1代表保持原有的质量(默认1) 范围 1 - 0
     *
     * @param filePath
     * @param scale
     * @param outputQualitySize
     * @throws IOException
     */
    public static void outputQuality(String filePath, float scale, float outputQualitySize) throws IOException {

        Thumbnails.of(filePath).scale(scale).outputQuality(outputQualitySize).toFile(filePath);

    }


    public static void keepAspectRatio(String filePath, int width, int height) throws IOException {

        // BufferedImage backgroundImage = Thumbnails.of("C:\\Users\\Jzdsh\\Desktop\\项目\\ss\\bj.jpg").size(150, 50).asBufferedImage();

        Thumbnails.of(filePath).size(width, height).toFile(filePath);


    }


    public static BufferedImage resize(int targetWidth, int targetHeight, BufferedImage src) {
        double scaleW = (double) targetWidth / (double) src.getWidth();
        double scaleH = (double) targetHeight / (double) src.getHeight();

        double scale = scaleW < scaleH ? scaleW : scaleH;

        BufferedImage result = new BufferedImage((int) (src.getWidth() * scale), (int) (src.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(src, 0, 0, result.getWidth(), result.getHeight(), null);
        g2d.dispose();

        return result;
    }

    public static String analysisReadQRCode(String path) {
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");
        try {
            URL url = new URL(path);
            BufferedImage source = ImageIO.read(url);
            BinaryBitmap binaryImg = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(source)));
            Result result = multiFormatReader.decode(binaryImg, hints);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 合成图片
     *
     * @param url      二维码内容
     * @param qrcodeW  二维码宽度
     * @param qrcodeH  二维码高度
     * @param backW    背景最大宽
     * @param backH    背景最大高
     * @param backFile 背景图文件
     * @param x        二维码的位置x
     * @param y        二维码的位置y
     * @param saveFile 合成保存的文件
     * @param saveFile 临时存放目录
     */
    public static void compositeByQrCodeUrl(String url, int qrcodeW, int qrcodeH, int backW, int backH, String backFile, String saveFile, int x, int y,

                                            String tempDir) {

        try {
            String qrCodeFile = tempDir + File.separator + IdGen.uuid() + ".jpg";
            QRCodeUtil.encode(url, qrCodeFile, qrcodeW, qrcodeH);

            BufferedImage bi1 = ImageIO.read(new File(backFile));
            BufferedImage bi2 = ImageIO.read(new File(qrCodeFile));
            if (bi2.getHeight() > qrcodeH || bi2.getWidth() > qrcodeW) {
                bi2 = resize(qrcodeW, qrcodeH, bi2);
            }
            if (bi1.getHeight() > backH || bi1.getWidth() > backW) {
                bi1 = resize(backW, backH, bi1);
            }

            Graphics2D g = bi1.createGraphics();

            g.drawImage(bi2, x, y, null);
            g.dispose();

            File outputFile = new File(saveFile);
            //此处要写PNG否则可能会出现遮罩层
            ImageIO.write(bi1, "png", outputFile);

            FileUtil.deleteFile(qrCodeFile);


        } catch (Exception e) {

        }


    }







    public static void main(String[] args) {


        try {

            String backFile = "/Users/sunke/Desktop/aa/1111.jpg";
            String aa = "/Users/sunke/Desktop/aa/11111111.jpg";
            String saveFile = "/Users/sunke/Desktop/aa/22222.png";
            String tempDir = "/Users/sunke/Desktop/aa/";


            compositeByQrCodeUrl("dddd", 102, 102, 300, 450, backFile, saveFile, 99, 310, tempDir);


            //   keepAspectRatio(aa,300,450);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("结束");
    }








}
